package com.winit.common.sg;

/**
 * Created by wanglin on 2018/5/11.
 */
import com.alibaba.fastjson.JSONObject;
import com.sun.javadoc.*;
import com.sun.tools.javadoc.Main;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class JavaDocReader {
    private static RootDoc root;
    // 一个简单Doclet,收到 RootDoc对象保存起来供后续使用
    // 参见参考资料6
    public static  class Doclet {

        public Doclet() {
        }
        public static boolean start(RootDoc root) {
            JavaDocReader.root = root;
            return true;
        }
    }
    public static String fileComments (String path){
        String rm="";
        try {
            Main.execute(new String[] {"-doclet",
                    Doclet.class.getName(),
                    "-encoding","utf-8",
                    path});
            ClassDoc[] classes = root.classes();
            for (int i = 0; i < classes.length; ++i) {
                if(!StringUtils.isEmpty(classes[i].commentText())){
                    rm=classes[i].commentText();
                }
            }
        }catch (Exception e){
        }
        return rm;
    }

    // 显示DocRoot中的基本信息
    public static List<JSONObject> method(String path){
        List<JSONObject> rmList = new ArrayList<>();
        try {
            Main.execute(new String[] {"-doclet",
                    Doclet.class.getName(),
                    "-encoding","utf-8",
                    path});
            ClassDoc[] classes = root.classes();
            for (int i = 0; i < classes.length; ++i) {
                for(MethodDoc method:classes[i].methods()){
                    JSONObject rm = new JSONObject();
                    rm.put("name",method.name());
                    rm.put("remarks",method.commentText());
                    rmList.add(rm);
                }
            }
        }catch (Exception e){
        }
        return rmList;
    }

    public static JSONObject fields(String path){
        JSONObject json=new JSONObject();
        List<JSONObject> rmList = new ArrayList<>();
        Main.execute(new String[] {"-doclet",
                Doclet.class.getName(),
                "-encoding","utf-8",
                path});
        ClassDoc[] classes = root.classes();
        for (int i = 0; i < classes.length; ++i) {
            if(!StringUtils.isEmpty(classes[i].commentText())){
                json.put("fileRemark",classes[i].commentText());
            }
            for(FieldDoc field:classes[i].fields(false)){
                if(!StringUtils.isEmpty(field.commentText())){
                    JSONObject rm = new JSONObject();
                    rm.put("name",field.name());
                    rm.put("remarks",field.commentText());
                    rmList.add(rm);
                }
            }
        }
        json.put("rmList",rmList);
        return json;
    }
    public static RootDoc getRoot() {
        return root;
    }
    public JavaDocReader() {

    }
    public static void main(final String ... args) throws Exception{
        // 调用com.sun.tools.javadoc.Main执行javadoc,参见 参考资料3
        // javadoc的调用参数，参见 参考资料1
        // -doclet 指定自己的docLet类名
        // -classpath 参数指定 源码文件及依赖库的class位置，不提供也可以执行，但无法获取到完整的注释信息(比如annotation)
        // -encoding 指定源码文件的编码格式
//        method();

        String te = fileComments("/Users/wanglin/JavaProject/spi/src/main/java/com/winit/pms/spi/winit/WinitServiceCatalogService.java");
        System.out.println(te);

    }
}